package nl.arjanfrans.stopwatch.fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Button;
import nl.arjanfrans.stopwatch.R;

import nl.arjanfrans.stopwatch.main.Stopwatch;

public class StopwatchFragment extends Fragment {
    private TextView elapsedTime_text;
    private Stopwatch stopwatch;
    private Button button_start_stop;
    private Button button_reset;
    private Button button_add_lap;
    private Handler handler;
    private final int update_interval = 100;

    private LapsFragments laps_activity;

    public StopwatchFragment(Stopwatch stopwatch, LapsFragments laps_activity) {
        this.stopwatch = stopwatch;
        this.laps_activity = laps_activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stopwatch, container, false);

        elapsedTime_text = (TextView) view.findViewById(R.id.elapsedTime_text);
        button_start_stop = (Button) view.findViewById(R.id.button_start_stop);
        button_start_stop.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onStartStopClicked(view);
                    }
                }
        );

        button_reset = (Button) view.findViewById(R.id.button_reset);
        button_reset.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onResetClicked(view);
                    }
                }
        );

        button_add_lap = (Button) view.findViewById(R.id.button_add_lap);
        button_add_lap.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddLapClicked(view);
                    }
                }
        );


        handler = new Handler();
        handler.postDelayed(update_view, update_interval);
        return view;
    }

    private Runnable update_view = new Runnable() {
        public void run() {
        if(!stopwatch.isPaused()) {
            elapsedTime_text.setText(stopwatch.getTimeText());
            handler.postDelayed(this, update_interval);
        }
        }
    };

    public void onStartStopClicked(View view) {
        if(stopwatch.isPaused()) {
            button_start_stop.setText(getString(R.string.stop));
            stopwatch.start();
            handler.postDelayed(update_view, update_interval);
        }
        else {
            button_start_stop.setText(getString(R.string.start));
            stopwatch.pause();
            elapsedTime_text.setText(stopwatch.getTimeText());
        }
    }

    public void onAddLapClicked(View view) {
        laps_activity.add_lap();
    }

    private void reset_alert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
        builder.setTitle(getString(R.string.reset));
        builder.setMessage(getString(R.string.reset_message));
        builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            reset_stopwatch();
            }
        });
        builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            //nothing
            }
        });
        builder.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        handler.postDelayed(update_view, update_interval);
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(update_view);
    }

    private void reset_stopwatch() {
        button_start_stop.setText(getString(R.string.start));
        elapsedTime_text.setText("00:00:00:000");
        stopwatch.reset();
        laps_activity.clear_laps();
        handler.removeCallbacks(update_view);
    }

    public void onResetClicked(View view) {
        reset_alert();
    }


}
